package com.ztjava.article.service;

import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.ztjava.article.model.ArticleCatalog;
import com.ztjava.article.model.ArticleComment;
import com.ztjava.article.model.ArticleContent;
import com.ztjava.core.base.BaseService;
import com.ztjava.core.client.Client;


public class ArticleService extends BaseService{
	public List<ArticleCatalog> getSubCatalogList(Integer parent_id){
		return ArticleCatalog.dao.find("select * from "+ArticleCatalog.tablename+" where parent_id=? and enable_flag='T'",parent_id);
	}
	public ArticleCatalog getCatalog(String custom){
		return ArticleCatalog.dao.findFirst("select * from "+ArticleCatalog.tablename+" where custom=? and enable_flag='T'",custom);
	}
	
	public Page<ArticleContent> contentPage(Client client,Map<String,String[]> paramMap){
		String[][] param = new String[][]{
				new String[]{"num",like_left,"B.num"},
				new String[]{"title",like,"A.title"}
				,new String[]{"enable_flag",eq,"A.enable_flag"}
		};
		Page<ArticleContent> pd = ArticleContent.dao.paginate(
				getParaToInt(paramMap,client.getPageNumber(), 1),
				getParaToInt(paramMap,client.getPageSize(), 20),
				"select A.*,B.name as catalog_name",
				"from "+ArticleContent.tablename+" A left join "+ArticleCatalog.tablename+" B ON A.catalog_id=B.id where 1=1 " 
				+whereSql(paramMap, param)
				+orderby(client,paramMap),whereParam(paramMap, param));
		changeBasedata(pd.getList(), new String[]{"enable_flag","ENABLE_FLAG"},
				new String[]{"is_comment","YESNO"});
		changeDatetime(pd.getList(), new String[]{"create_time","yyyy/MM/dd"});
		return pd;
	}
	//更新文章评论量
	public void updateContentComment(Integer id){
		int amount = Db.queryInt("select count(id) as amount from "+ArticleComment.tablename+" where content_id=?",id);
		Db.update("update "+ArticleContent.tablename+" set comment=? where id=?",amount,id);
	}
	

	public Page<ArticleComment> getCommentPage(Client client,Map<String,String[]> paramMap){
		String[][] param = new String[][]{
				new String[]{"content_id",eq,"content_id"},
				new String[]{"content",like,"content"}
				,new String[]{"enable_flag",eq,"enable_flag"}
		};
		Page<ArticleComment> pd = ArticleComment.dao.paginate(
				getParaToInt(paramMap,client.getPageNumber(), 1),
				getParaToInt(paramMap,client.getPageSize(), 20),
				"select *",	"from "+ArticleComment.tablename+" where 1=1 " 
				+whereSql(paramMap, param)
				+orderby(client,paramMap),whereParam(paramMap, param));
		changeBasedata(pd.getList(), new String[]{"enable_flag","ENABLE_FLAG"});
		changeUser(pd.getList(), new String[]{"create_user","name"},new String[]{"reply_user","name"});
		changeDatetime(pd.getList(), new String[]{"create_time","yyyy/MM/dd HH:mm"});
		return pd;
	}
}
